package com.example.demo.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Map;

public class JsonUtil {
    // 定义jackson对象
    private static final ObjectMapper MAPPER = new ObjectMapper();
    public static JsonUtil instance;
    private static Gson gson;

    static {
        gson = new Gson();
    }

    public static JsonUtil getInstance() {       //单例引用
        if (instance == null) {
            synchronized (JsonUtil.class) {
                if (instance == null) {
                    instance = new JsonUtil();
                    synchronized (Gson.class) {
                        gson = new Gson();
                    }
                }
            }
        }
        return instance;
    }

    public <T> String beanToJsonStr(T t) {        //将对象转换为json的字符串
        String str = "";
        if (t != null) {
            str = gson.toJson(t);
        }
        return str;
    }

    public <T> T jsonToBean(String jsonStr, Class<T> tClass) {   //将json格式的字符转换为对象
        T t = null;
        if (!(jsonStr == null || "".equals(jsonStr))) {
            try {
                t = gson.fromJson(jsonStr, tClass);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return t;
    }

    public static <T> ArrayList<T> jsonToArrayList(String json,
                                                   Class<T> clazz) {    //将json的字符转换为对象数组
        Type type = new TypeToken<ArrayList<JsonObject>>() {
        }.getType();
        ArrayList<JsonObject> list1 = gson.fromJson(json, type);
        ArrayList<T> list = new ArrayList<>();
        list1.forEach(l -> {
            list.add(gson.fromJson(l, clazz));
        });
        return list;
    }


    /**
     * json字符串转化为Map<String,String>
     *
     * @param jsonStr json字符串
     * @return map
     */
    public static Map<String, String> jsonToMap(String jsonStr) {
        Map<String, String> map = null;
        if (!(jsonStr == null || "".equals(jsonStr))) {
            try {
                Type mapType = new TypeToken<Map<String, String>>() {
                }.getType();
                map = gson.fromJson(jsonStr, mapType);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return map;
    }

    public static <T> T jsonToPojo(String jsonData, Class<T> beanType) {
        try {
            MAPPER.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
            T t = MAPPER.readValue(jsonData, beanType);
            return t;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 序列化對象為json字符�?	 *
     *
     * @param t 泛型對象
     * @return
     */
    public <T> String serializeObject(T t) {
        String str = "";
        if (t != null) {
            Gson gson = new Gson();
            try {
                if (t.getClass().getName().endsWith("String")) {
                    str = (String) t;
                } else {
                    str = gson.toJson(t);
                }
            } catch (Exception e) {
                // TODO: handle exception
            }
        }
        return str;
    }


}
